package top.yuzt.animalserver.common;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class GiteeService {

    private final OkHttpClient httpClient = new OkHttpClient();

    public String getCommits(String owner, String repo, String token) throws IOException {
        String url = String.format("https://gitee.com/api/v5/repos/%s/%s/commits", owner, repo);

        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", "token " + token)
                .build();

        try (Response response = httpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            return response.body().string();
        }
    }
}
